package email

import (
	"gf_email_task/app/service/email"
	"gf_email_task/library/response"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gvalid"
)

// 用户API管理对象
type C struct{}

// 邮件发送接口
func (c *C) Sender(r *ghttp.Request) {
	var (
		emailServiceParam *email.EmailServiceParam
	)
	if err := r.GetStruct(&emailServiceParam); err != nil {
		response.JsonExit(r, response.ERROR1 , err.Error())
	}

	if err := gvalid.CheckStruct(&emailServiceParam,nil); err != nil {
		response.JsonExit(r, response.ERROR2, err.FirstString())
	}

	emailServiceParam.Host = r.Request.Host

	if err := email.EmailService(emailServiceParam); err != nil {
		response.JsonExit(r, response.ERROR3, err.Error())
	} else {
		response.JsonExit(r, response.SUCCESS, "ok")
	}
}

// 邮件发送接口
func (c *C) Logo(r *ghttp.Request) {
	var (
		emailViewParam *email.EmailViewParam
	)
	if err := r.Parse(&emailViewParam); err == nil {
		email.EmailViewService(emailViewParam)
	}
	r.Response.Header().Set("Content-Type", "image/png")
	r.Response.Write("")
	r.Exit()
}

// 获取支持模板列表
func (c *C) Template(r *ghttp.Request) {
	if res,err := email.EmailTemplateService(); err != nil {
		response.JsonExit(r, response.ERROR1, err.Error())
	} else {
		response.JsonExit(r, response.SUCCESS, "ok",res)
	}
}